package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 15:43 2021/10/9
 */

import com.liu.community.dto.BlogDto;
import com.liu.community.dto.NotificationDto;
import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.pojor.User;
import com.liu.community.server.BlogService;
import com.liu.community.server.NotificationService;
import com.liu.community.server.QuestionService;
import com.liu.community.server.UserService;
import com.liu.community.utils.URLUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ProfileController {

    @Autowired
    private NotificationService notificationService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/profile/{action}")
    public  String goProfile(@PathVariable(name = "action")String action, Model model,
                             HttpServletRequest request,
                             @RequestParam(name = "currentPage",defaultValue = "1")Integer currentPage,
                             @RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            return URLUtils.sendRedirectTo("/index");
        }
        if ("questions".equals(action)) {
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的提问");
            PageDto<QuestionDto> PageDto = questionService.list(user.getId(), currentPage, pageSize);
            model.addAttribute("pageList",PageDto);
        } else if ("reply".equals(action)) {
            model.addAttribute("section", "reply");
            model.addAttribute("sectionName", "最新回复");
            PageDto<NotificationDto> PageDto = notificationService.list(user.getId(), currentPage, pageSize);
            model.addAttribute("pageList",PageDto);
        }else if ("blog".equals(action)){
            model.addAttribute("section","blog");
            model.addAttribute("sectionName","我的博客");
            PageDto<BlogDto> PageDto = blogService.list(user.getId(), currentPage, pageSize);
            model.addAttribute("pageList",PageDto);
        }

        return "profile";
    }
}
